//-----------------------------------------------------------------------
// <copyright file="SettingsManager.cs" company="n.a.">
//     Copyright Dave999. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "To be returned to .")]

/*
    New for 2012: Module change log
    Moving this class to a "resource" pattern 
    ie. private constructor and a single instance, Special resources Namespace

    Settings are held in the <Settings> element of the main config file
    They are organised into separate namespaces for future development
    You could imagine a standard tabbed options setting dialog where each
    tab supported a single settings namespace.

    No need to load as the config file is loaded at boot-up
*/
namespace DbView.Resources
{
	using System;
	using System.Collections;
	using System.IO;
	using System.Text.RegularExpressions;
	using System.Xml;
	using System.Collections.Generic;
	/// <summary>
	/// Manages the list of connections
	/// </summary>
	// Should be renamed when it works
	public class SettingsMgr
	{
		// the singleton object
		static SettingsMgr Implementation = null;

		// access to the singleton
		public static SettingsMgr Instance
		{
			get
			{
				if (Implementation == null)
					Implementation = new SettingsMgr();
				return Implementation;
			}
		}

		public SettingsMgr()
		{
		}

		public string GetValue(string nameSpace, string settingName)
		{
			string xpath = string.Format("//{0}/{1}/{2}", ConfigDoc.CFGELM_SETTINGS, nameSpace, settingName);
			XmlNode settingNode = ConfigDoc.Instance.SelectSingleNode(xpath);
			if (settingNode != null) return settingNode.InnerText;
			return "";
		}

		public void SetValue(string nameSpace, string settingName, string value)
		{
			XmlNode nsNode = this.SettingsNs(nameSpace);
			string xpath = settingName;
			XmlNode settingNode = nsNode.SelectSingleNode(xpath);
			if (settingNode == null)
			{
				settingNode = nsNode.OwnerDocument.CreateElement(settingName);
				nsNode.AppendChild(settingNode);
			}
			settingNode.InnerText = value;
		}

		public bool Load()
		{
			// no need to load as it is done with load of main config file
			// here in case they are moved out of the main config file.
			return true;
		}

		public void Save()
		{
			// for future developments
			ConfigDoc.Instance.SaveConfig();
		}

		private XmlNode SettingsNs(string nameSpace)
		{
			string xpath = string.Format("//{0}/{1}", ConfigDoc.CFGELM_SETTINGS, nameSpace);
			XmlNode namespaceNode = ConfigDoc.Instance.SelectSingleNode(xpath);
			if (namespaceNode == null)
			{
				xpath = string.Format("//{0}", ConfigDoc.CFGELM_SETTINGS);
				XmlNode settingsNode = this.SettingsNode();
				namespaceNode = settingsNode.OwnerDocument.CreateElement(nameSpace);
				XmlNode first = settingsNode.FirstChild;
				if (first == null)
					settingsNode.AppendChild(namespaceNode);
				else
					settingsNode.InsertBefore(settingsNode, first);
			}
			return namespaceNode;
		}
		private XmlNode SettingsNode()
		{
			// search doc for settings
			string xpath = string.Format("//{0}", ConfigDoc.CFGELM_SETTINGS);
			XmlNode settingsNode = ConfigDoc.Instance.SelectSingleNode(xpath);
			if (settingsNode == null)
			{
				// search for doc element.
				xpath = string.Format("//{0}", ConfigDoc.CFGELM_DOC);
				XmlNode docNode = ConfigDoc.Instance.SelectSingleNode(xpath);
				// bootstrap. Ignore settings for now. 
				if (docNode == null) return null;
				settingsNode = docNode.OwnerDocument.CreateElement(ConfigDoc.CFGELM_SETTINGS);
				XmlNode first = docNode.FirstChild;
				if (first == null)
					docNode.AppendChild(settingsNode);
				else
					docNode.InsertBefore(settingsNode, first);
			}
			return settingsNode;
		}

	}
}
